package safeCommitter.utils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import safeCommitter.model.MMethod;
import safeCommitter.model.TestType;

public class SCUtils {

	public static MMethod createSCMethod(final IMethod m)
			throws JavaModelException {
		MMethod method = new MMethod();
		method.setName(m.getElementName());
		List<String> parameter = method.getParameter();
		parameter.addAll(Arrays.asList(m.getParameterTypes()));
		return method;
	}

	public static IJavaSearchScope buildScope(
			final IPackageFragmentRoot[] packageRoots) {
		IPackageFragmentRoot[] packageList = packageRoots.clone();
		for (int i = 0; i < packageRoots.length; i++) {
			if (packageRoots[i].isArchive()) {
				packageList[i] = null;
			}
		}

		return SearchEngine.createJavaSearchScope(packageList, false);
	}

	public static boolean isTestMethod(final IMethod method) {
		try {
			return method.getSource().contains("@Test");
		} catch (JavaModelException e) {
			return false;
		}
	}

	public static TestType getTestType(final IMethod m) {
		ICompilationUnit unit = m.getCompilationUnit();
		IImportDeclaration junitImport = unit.getImport("org.junit.Test");
		IImportDeclaration testNGImport = unit
				.getImport("org.testng.annotations.Test");
		if (junitImport.exists() && testNGImport.exists()) {
			return TestType.INDETERMINED;
		} else if (junitImport.exists()) {
			return TestType.JUNIT;
		} else if (testNGImport.exists()) {
			return TestType.TESTNG;
		}
		return TestType.WRONGTYPE;
	}

	public static IVMInstall getVMInstall(final IJavaProject project)
			throws CoreException {
		IVMInstall vmInstall = JavaRuntime.getVMInstall(project);
		if (vmInstall == null)
			vmInstall = JavaRuntime.getDefaultVMInstall();
		return vmInstall;
	}

	public static Shell getShell() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		if (window == null)
			return null;
		return window.getShell();
	}

	public static IWorkbenchPage getPage() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		return window.getActivePage();
	}

	public static ICompilationUnit getJavaElement(final IFile file) {
		IJavaElement ije = JavaCore.create(file);
		return (ICompilationUnit) ije.getAncestor(5);
	}

	public static void log(String text) {
		File file = new File("sclog.txt");
		try {
			file.createNewFile();
			FileUtils.writeStringToFile(file, text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
